
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 06/12/2011 14:28:38
-- Generated from EDMX file: D:\Users\Anthyme\Documents\Visual Studio 2010\Projects\ArchSharp\Samples\Shared\Arch.Samples.Mapping\SamplesModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [Arch];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_TodoUser]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Todoes] DROP CONSTRAINT [FK_TodoUser];
GO
IF OBJECT_ID(N'[dbo].[FK_SubCategoryGlobalCategory]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Categories_SubCategory] DROP CONSTRAINT [FK_SubCategoryGlobalCategory];
GO
IF OBJECT_ID(N'[dbo].[FK_TodoGlobalCategory]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Todoes] DROP CONSTRAINT [FK_TodoGlobalCategory];
GO
IF OBJECT_ID(N'[dbo].[FK_TodoSubCategory]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Todoes] DROP CONSTRAINT [FK_TodoSubCategory];
GO
IF OBJECT_ID(N'[dbo].[FK_SubCategory_inherits_Category]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Categories_SubCategory] DROP CONSTRAINT [FK_SubCategory_inherits_Category];
GO
IF OBJECT_ID(N'[dbo].[FK_GlobalCategory_inherits_Category]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Categories_GlobalCategory] DROP CONSTRAINT [FK_GlobalCategory_inherits_Category];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Todoes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Todoes];
GO
IF OBJECT_ID(N'[dbo].[Users]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Users];
GO
IF OBJECT_ID(N'[dbo].[Categories]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Categories];
GO
IF OBJECT_ID(N'[dbo].[Categories_SubCategory]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Categories_SubCategory];
GO
IF OBJECT_ID(N'[dbo].[Categories_GlobalCategory]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Categories_GlobalCategory];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Todoes'
CREATE TABLE [dbo].[Todoes] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Title] nvarchar(max)  NOT NULL,
    [Description] nvarchar(max)  NOT NULL,
    [Version] int  NOT NULL,
    [CreationDate] datetime  NOT NULL,
    [UpdateDate] datetime  NOT NULL,
    [CreationUserId] int  NOT NULL,
    [GlobalCategoryId] int  NOT NULL,
    [SubCategoryId] int  NOT NULL
);
GO

-- Creating table 'Users'
CREATE TABLE [dbo].[Users] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Username] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Categories'
CREATE TABLE [dbo].[Categories] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [Name] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'Categories_SubCategory'
CREATE TABLE [dbo].[Categories_SubCategory] (
    [GlobalCategoryId] int  NOT NULL,
    [Id] int  NOT NULL
);
GO

-- Creating table 'Categories_GlobalCategory'
CREATE TABLE [dbo].[Categories_GlobalCategory] (
    [Id] int  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'Todoes'
ALTER TABLE [dbo].[Todoes]
ADD CONSTRAINT [PK_Todoes]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Categories'
ALTER TABLE [dbo].[Categories]
ADD CONSTRAINT [PK_Categories]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Categories_SubCategory'
ALTER TABLE [dbo].[Categories_SubCategory]
ADD CONSTRAINT [PK_Categories_SubCategory]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'Categories_GlobalCategory'
ALTER TABLE [dbo].[Categories_GlobalCategory]
ADD CONSTRAINT [PK_Categories_GlobalCategory]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [CreationUserId] in table 'Todoes'
ALTER TABLE [dbo].[Todoes]
ADD CONSTRAINT [FK_TodoUser]
    FOREIGN KEY ([CreationUserId])
    REFERENCES [dbo].[Users]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_TodoUser'
CREATE INDEX [IX_FK_TodoUser]
ON [dbo].[Todoes]
    ([CreationUserId]);
GO

-- Creating foreign key on [GlobalCategoryId] in table 'Categories_SubCategory'
ALTER TABLE [dbo].[Categories_SubCategory]
ADD CONSTRAINT [FK_SubCategoryGlobalCategory]
    FOREIGN KEY ([GlobalCategoryId])
    REFERENCES [dbo].[Categories_GlobalCategory]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SubCategoryGlobalCategory'
CREATE INDEX [IX_FK_SubCategoryGlobalCategory]
ON [dbo].[Categories_SubCategory]
    ([GlobalCategoryId]);
GO

-- Creating foreign key on [GlobalCategoryId] in table 'Todoes'
ALTER TABLE [dbo].[Todoes]
ADD CONSTRAINT [FK_TodoGlobalCategory]
    FOREIGN KEY ([GlobalCategoryId])
    REFERENCES [dbo].[Categories_GlobalCategory]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_TodoGlobalCategory'
CREATE INDEX [IX_FK_TodoGlobalCategory]
ON [dbo].[Todoes]
    ([GlobalCategoryId]);
GO

-- Creating foreign key on [SubCategoryId] in table 'Todoes'
ALTER TABLE [dbo].[Todoes]
ADD CONSTRAINT [FK_TodoSubCategory]
    FOREIGN KEY ([SubCategoryId])
    REFERENCES [dbo].[Categories_SubCategory]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_TodoSubCategory'
CREATE INDEX [IX_FK_TodoSubCategory]
ON [dbo].[Todoes]
    ([SubCategoryId]);
GO

-- Creating foreign key on [Id] in table 'Categories_SubCategory'
ALTER TABLE [dbo].[Categories_SubCategory]
ADD CONSTRAINT [FK_SubCategory_inherits_Category]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[Categories]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id] in table 'Categories_GlobalCategory'
ALTER TABLE [dbo].[Categories_GlobalCategory]
ADD CONSTRAINT [FK_GlobalCategory_inherits_Category]
    FOREIGN KEY ([Id])
    REFERENCES [dbo].[Categories]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------